package jp.co.toshiba.ffsa.action.csas;

import java.util.ArrayList;

import javax.annotation.Resource;

import jp.co.toshiba.ffsa.action.common.CommonAction;
import jp.co.toshiba.ffsa.dto.LoginDto;
import jp.co.toshiba.ffsa.form.csas.Csas402Form;
import jp.co.toshiba.ffsa.service.common.ConstantValue;
import jp.co.toshiba.ffsa.service.csas.Csas402Service;

import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.seasar.struts.annotation.ActionForm;
import org.seasar.struts.annotation.Execute;
import org.seasar.struts.util.ActionMessagesUtil;
import org.seasar.struts.util.MessageResourcesUtil;
import org.seasar.struts.util.RequestUtil;

/**
 * (パスワード変更警告)パスワード変更入力
 * @author npc
 */
public class Csas402Action extends CommonAction {
    /** 画面ID */
    public String screenID = "CSAS402";

    /** ActionForm */
    @ActionForm
    @Resource
    protected Csas402Form csas402Form;

    /**
     * 初期処理
     * @return 画面遷移先
     */
    @Execute(validator = false)
    public String index() throws Exception{
        try{
            // リファラ判定
            if(this.judgeRefferer()){
                // セッションタイムアウト判定
                if(this.judgeSession(csas402Form.getSessionId())){
                    this.csas402Form.reset();
                }else{
                    return this.topTransition;
                }
            }else{
                return this.topTransition;
            }
        }catch(Exception e){
            this.errorLogWriter(e, "index", "CSAS402");
            throw e;
        }
        return "/csas/csas402.jsp";
    }

    /**
     * パスワード変更処理
     * @return 画面遷移先
     */
    @Execute(validator = true, input="/csas/csas402")
    public String save() throws Exception{
        String transition = "/csas/csas402.jsp"; // 遷移先
        String result = "";
        try{
            // リファラ判定
            if(this.judgeRefferer()){
                // セッションタイムアウト判定
                if(this.judgeSession(csas402Form.getSessionId())){
                    Csas402Service service = new Csas402Service();
                    service.setUserID(((LoginDto)RequestUtil.getRequest().getSession().getAttribute("login_dto")).getUserID());
                    service.setCurrentPassword(this.csas402Form.currentPassword);
                    service.setNewPassword(this.csas402Form.newPassword);
                    if(service.savePassword()){
                        /*transition = "/csas/csas403.jsp";*/
                        transition = "/cstp/cstp101/passwordChanged";
                        result = ConstantValue.success;
                    }else{
                        ActionMessages messages = new ActionMessages();
                        messages.add("entryFailed", new ActionMessage(service.getErrorCode(), true));
                        ActionMessagesUtil.addErrors(RequestUtil.getRequest().getSession(true), messages);
                        transition = "/csas/csas402.jsp";
                        result = ConstantValue.fail;
                    }

                    if(ConstantValue.success.equals(result)){
                        // 操作ログ記述
                        ArrayList<ArrayList<String>> parameters = new ArrayList<ArrayList<String>>();
                        ArrayList<String> parameterNames = new ArrayList<String>();
                        ArrayList<String> values = new ArrayList<String>();
                        parameterNames.add(MessageResourcesUtil.getMessage("current_password"));
                        values.add("-");
                        parameterNames.add(MessageResourcesUtil.getMessage("new_password"));
                        values.add("-");
                        parameterNames.add(MessageResourcesUtil.getMessage("Reenter_new_password"));
                        values.add("-");
                        parameters.add(parameterNames);
                        parameters.add(values);
                        this.entryOperationHistory(this.screenID, "submit", parameters, result);
                    }

                    return transition;

                }else{
                    return this.topTransition;
                }
            }else{
                return this.topTransition;
            }
        }catch(Exception e){
            this.errorLogWriter(e, "save", "CSAS402");
            throw e;
        }
    }
}